﻿@model ProductModel
@using Telerik.Web.Mvc.UI;

<script>

	var fieldBasePriceMeasureUnit;
	var fieldBasePriceAmount;
	var fieldBasePriceBaseAmount;

	$(function () {
		fieldBasePriceMeasureUnit = $("#@Html.FieldIdFor(model => model.BasePriceMeasureUnit)");
		fieldBasePriceAmount = $("#@Html.FieldIdFor(model => model.BasePriceAmount)");
		fieldBasePriceBaseAmount = $("#@Html.FieldIdFor(model => model.BasePriceBaseAmount)");

		fieldBasePriceMeasureUnit.on("change", getCurrentBasePrice);
		fieldBasePriceAmount.on("blur", getCurrentBasePrice);
		fieldBasePriceBaseAmount.on("blur", getCurrentBasePrice);

		getCurrentBasePrice();

		function getCurrentBasePrice() {
			var basePriceAmount = SmartStore.globalization.parseFloat(fieldBasePriceAmount.val());
			var basePriceBaseAmount = SmartStore.globalization.parseInt(fieldBasePriceBaseAmount.val());
			if (basePriceAmount > 0 && basePriceBaseAmount > 0) {
				$.ajax({
					cache:false,
					type: "POST",
					url: "@(Url.Action("GetBasePrice", "Product"))",
					data: {
						"productId" : @Model.Id,
						"basePriceMeasureUnit": fieldBasePriceMeasureUnit.val(),
						"basePriceAmount": fieldBasePriceAmount.val(),
						"basePriceBaseAmount": fieldBasePriceBaseAmount.val()
					},
					success: function (data) {
						$("#BasePriceAmountInfo").html(data.BasePrice);
					},
					error: function (xhr, ajaxOptions, thrownError){
						//alert(thrownError);
					}
				});
			}
			return false;
		}
	})
</script>

<div id="group-prices">
    <div class="adminContent">
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.Price)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.Price, new { postfix = Model.PrimaryStoreCurrencyCode })
                @Html.ValidationMessageFor(model => model.Price)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.OldPrice)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.OldPrice, new { postfix = Model.PrimaryStoreCurrencyCode })
                @Html.ValidationMessageFor(model => model.OldPrice)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.ProductCost)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.ProductCost, new { postfix = Model.PrimaryStoreCurrencyCode })
                @Html.ValidationMessageFor(model => model.ProductCost)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.SpecialPrice)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.SpecialPrice, new { postfix = Model.PrimaryStoreCurrencyCode })
                @Html.ValidationMessageFor(model => model.SpecialPrice)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.SpecialPriceStartDateTimeUtc)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.SpecialPriceStartDateTimeUtc, new { pickTime = true })
                @Html.ValidationMessageFor(model => model.SpecialPriceStartDateTimeUtc)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.SpecialPriceEndDateTimeUtc)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.SpecialPriceEndDateTimeUtc, new { pickTime = true })
                @Html.ValidationMessageFor(model => model.SpecialPriceEndDateTimeUtc)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.DisableBuyButton)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.DisableBuyButton)
                @Html.ValidationMessageFor(model => model.DisableBuyButton)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.DisableWishlistButton)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.DisableWishlistButton)
                @Html.ValidationMessageFor(model => model.DisableWishlistButton)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.AvailableForPreOrder)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.AvailableForPreOrder)
                @Html.ValidationMessageFor(model => model.AvailableForPreOrder)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.CallForPrice)
            </div>
            <div class="adminData">
                @Html.EditorFor(model => model.CallForPrice)
                @Html.ValidationMessageFor(model => model.CallForPrice)
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.CustomerEntersPrice)
            </div>
            <div class="adminData">
                @Html.CheckBoxFor(model => model.CustomerEntersPrice, new { data_toggler_for = "#pnlCustomerEntersPrice" })
                @Html.ValidationMessageFor(model => model.CustomerEntersPrice)
            </div>
        </div>
		<div id="pnlCustomerEntersPrice">
			<div class="adminRow">
				<div class="adminTitle">
					@Html.SmartLabelFor(model => model.MinimumCustomerEnteredPrice)
				</div>
				<div class="adminData">
					@Html.EditorFor(model => model.MinimumCustomerEnteredPrice, new { postfix = Model.PrimaryStoreCurrencyCode })
					@Html.ValidationMessageFor(model => model.MinimumCustomerEnteredPrice)
				</div>
			</div>
			<div class="adminRow">
				<div class="adminTitle">
					@Html.SmartLabelFor(model => model.MaximumCustomerEnteredPrice)
				</div>
				<div class="adminData">
					@Html.EditorFor(model => model.MaximumCustomerEnteredPrice, new { postfix = Model.PrimaryStoreCurrencyCode })
					@Html.ValidationMessageFor(model => model.MaximumCustomerEnteredPrice)
				</div>
			</div>
		</div>
        <div class="adminSeparator">
			<hr />
        </div>
        <!-- PAnGV -->
        <div class="adminRow">
            <div class="adminTitle">
                @Html.SmartLabelFor(model => model.BasePriceEnabled)
            </div>
            <div class="adminData">
                @Html.CheckBoxFor(model => model.BasePriceEnabled, new { data_toggler_for = "#pnlBasePriceEnabled" })
                @Html.ValidationMessageFor(model => model.BasePriceEnabled)
            </div>
        </div>
		<div id="pnlBasePriceEnabled">
			<div class="adminRow">
				<div class="adminTitle">
					@Html.SmartLabelFor(model => model.BasePriceBaseAmount)
				</div>
				<div class="adminData">
					<div class="form-row">
						<div class="col">
							@Html.EditorFor(model => model.BasePriceBaseAmount)
						</div>
						<div class="col-auto" style="width: 120px">
							@Html.DropDownListFor(model => model.BasePriceMeasureUnit, Model.AvailableMeasureUnits)
						</div>
					</div>
				</div>
			</div>
			<div class="adminRow">
				<div class="adminTitle">
					@Html.SmartLabelFor(model => model.BasePriceAmount)
				</div>
				<div class="adminData">
					@Html.EditorFor(model => model.BasePriceAmount)
					<span id="BasePriceAmountInfo" class="d-inline-block text-muted pt-2">@*@T("Admin.Catalog.Products.Fields.BasePriceInfo")*@</span>
				</div>
			</div>
			<div class="adminRow">
				<div class="adminTitle">
					&nbsp;
				</div>
				<div class="adminData">
					<div class="alert alert-info">
						@T("Admin.Products.BasePrice.Hint")
					</div>
				</div>
			</div>
		</div>   
    </div>

    <div id="TierPriceContainer">
        <h4 class="mb-4">@T("Admin.Catalog.Products.TierPrices")</h4>

        @if (Model.Id > 0)
        {
            <script>
				function onTierPriceEdit(e) {
					if (e.mode == "edit") {
						$('#Store')
							.data('select-selected-id', e.dataItem['StoreId'])
							.data('select-init-text', e.dataItem['Store'])
							.val(e.dataItem['StoreId'])
							.trigger('change');

						$('#CustomerRole')
							.data('select-selected-id', e.dataItem['CustomerRoleId'])
							.data('select-init-text', e.dataItem['CustomerRole'])
							.val(e.dataItem['CustomerRoleId'])
							.trigger('change');

						$('#CalculationMethod')
							.data('select-selected-id', e.dataItem['CalculationMethodId'])
							.data('select-init-text', e.dataItem['CalculationMethod'])
							.val(e.dataItem['CalculationMethodId'])
							.trigger('change');
					}
                }
            </script>
			
			@(Html.Telerik().Grid<ProductModel.TierPriceModel>()
				.Name("tierprices-grid")
				.DataKeys(keys =>
				{
					keys.Add(x => x.Id);
				})
				.DataBinding(dataBinding =>
				{
					dataBinding.Ajax()
						.Select("TierPriceList", "Product", new { productId = Model.Id })
						.Insert("TierPriceInsert", "Product", new { productId = Model.Id })
						.Update("TierPriceUpdate", "Product")
						.Delete("TierPriceDelete", "Product");
				})
				.Columns(columns =>
				{
					columns.Bound(x => x.Quantity)
						.Centered()
						.Width(80);
					columns.Bound(x => x.Price1)
						.Format("{0:0.00}")
						.Width(120)
						.RightAlign();
					columns.Bound(x => x.CalculationMethod).Width("20%");
					columns.Bound(x => x.Store).Width("25%");
					columns.Bound(x => x.CustomerRole).Width("20%");
					columns.Command(commands =>
					{
						commands.Edit().Localize(T);
						commands.Delete().Localize(T);
					})
					//.Width(180)
					.HtmlAttributes(new { align = "right" });
				})
				.ToolBar(commands => commands.Insert())
				.ClientEvents(events => events.OnEdit("onTierPriceEdit"))
				.EnableCustomBinding(true))
        }
        else
        {
            <div class="alert alert-danger">
                @T("Admin.Catalog.Products.TierPrices.SaveBeforeEdit")
            </div>
        }
    </div>
</div>